package com.moehu.back.controller.app;

import com.alibaba.fastjson.JSONObject;
import com.moehu.back.annotation.AppRestController;
import com.moehu.back.annotation.UserLoginToken;
import com.moehu.back.model.OpusModel;
import com.moehu.back.model.ResultModel;
import com.moehu.back.model.TagModel;
import com.moehu.back.model.request.CreatedOpusInfoTagModel;
import com.moehu.back.model.response.PagingResponseModel;
import com.moehu.back.service.impl.TagServiceImpl;
import io.swagger.annotations.*;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;
import java.net.URLDecoder;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping(value = "/tag")
@AppRestController
@Api(tags = "Tag 标签相关的接口")
public class TagController {

    @Autowired
    TagServiceImpl tagService;

    @UserLoginToken
    @ApiOperation(value="【需要Token】删除标签", notes="主要是给用户用来删除标签")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tags_id", value = "被删除的标签id", required = true, dataType = "Number", defaultValue="1"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 500, message = "缺少token，请登录"),
            @ApiResponse(code = 500, message = "无效token，请勿伪造token"),
            @ApiResponse(code = 500, message = "用户不存在，请重新登录")
    })
    @PostMapping("/DeleteTag")
    public ResultModel deleteTag(@RequestBody Map<String, Integer> tag) {
        return tagService.deleteTag(tag.get("tags_id"));
    }

    @UserLoginToken
    @ApiOperation(value="【需要Token】编辑标签", notes="主要用来修改标签的文字，例如将：'二次元' 修改为 '二次元世界' ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tags_id", value = "被修改的标签id", required = true, dataType = "Number", defaultValue="1"),
            @ApiImplicitParam(name = "tags_title", value = "被修改成的文字", required = true, dataType = "String", defaultValue="二次元世界"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 500, message = "缺少token，请登录"),
            @ApiResponse(code = 500, message = "无效token，请勿伪造token"),
            @ApiResponse(code = 500, message = "用户不存在，请重新登录")
    })
    @PostMapping("/EditTag")
    public ResultModel editTag(@RequestBody Map<String, String> tag) {
        return tagService.editTag(Integer.parseInt(tag.get("tags_id")), tag.get("tags_title"));
    }


    @ApiOperation(value="获取推荐标签", notes="用户在发布作品的时候用来获取推荐的标签")
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 500, message = "缺少token，请登录"),
            @ApiResponse(code = 500, message = "无效token，请勿伪造token"),
            @ApiResponse(code = 500, message = "用户不存在，请重新登录")
    })
    @PostMapping("/RecommendTag")
    public ResultModel<List<TagModel>> RecommendTag() {
        return tagService.RecommendTag();
    }



    @UserLoginToken
    @ApiOperation(value="【需要Token】发布作品的时候创建标签接口", notes="发布作品的时候创建标签接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tag_arr", value = "标签名组成的数组，tag_arr 示例：['娱乐','原创','二次元']", required = true, dataType = "Array<String>", defaultValue="['大雄','静香','小夫','原创']"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 500, message = "缺少token，请登录"),
            @ApiResponse(code = 500, message = "无效token，请勿伪造token"),
            @ApiResponse(code = 500, message = "用户不存在，请重新登录")
    })
    @PostMapping("/CreatedTags")
    public ResultModel CreatedTags(@RequestBody Map<String, String[]> tag) {
        return tagService.createdTag(tag.get("tag_arr"));
    }


    @UserLoginToken
    @ApiOperation(value="【需要Token】作品详情页编辑标签接口", notes="作品详情页编辑标签接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tags_title", value = "中文标签名", required = true, dataType = "String", defaultValue="['大雄','静香','小夫','原创']"),
            @ApiImplicitParam(name = "user_id", value = "用户id", required = true, dataType = "Number"),
            @ApiImplicitParam(name = "opus_id", value = "作品id", required = true, dataType = "Number"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 500, message = "缺少token，请登录"),
            @ApiResponse(code = 500, message = "无效token，请勿伪造token"),
            @ApiResponse(code = 500, message = "用户不存在，请重新登录")
    })
    @PostMapping("/OpusInfoCreatedTags")
    public ResultModel OpusInfoCreatedTags(@RequestBody CreatedOpusInfoTagModel tag) {
        return tagService.createdOpusInfoTag(tag);
    }

    @UserLoginToken
    @ApiOperation(value="【需要Token】更新作品详情页是否允许编辑的状态", notes="更新 允许其他会员添加标签")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "opus_id", value = "作品id", required = true, dataType = "Number"),
            @ApiImplicitParam(name = "edit_status", value = "编辑的状态，1 表示开启允许编辑，0 表示关闭允许编辑",
                    required = true, dataType = "Number"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 500, message = "缺少token，请登录"),
            @ApiResponse(code = 500, message = "无效token，请勿伪造token"),
            @ApiResponse(code = 500, message = "用户不存在，请重新登录")
    })
    @PostMapping("/UpdateTagsEditStatus")
    public ResultModel UpdateTagsEditStatus(@RequestBody Map<String, Integer> tag) {
        return tagService.UpdateTagsEditStatus(tag);
    }


    @UserLoginToken
    @ApiOperation(value="【需要Token】获取用户创建的所有标签", notes="主要用来获取用户创建过的所有标签")
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 500, message = "缺少token，请登录"),
            @ApiResponse(code = 500, message = "无效token，请勿伪造token"),
            @ApiResponse(code = 500, message = "用户不存在，请重新登录")
    })
    @PostMapping("/UserTagList")
    public ResultModel<List<TagModel>> UserTagList() {
        return tagService.userTagList();
    }


    @ApiOperation(value="获取作品的标签", notes="作品详情页需要使用到做的标签功能")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "opus_id", value = "作品的opus_id 参数", required = true, dataType = "Number", defaultValue="1"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 500, message = "缺少token，请登录"),
            @ApiResponse(code = 500, message = "无效token，请勿伪造token"),
            @ApiResponse(code = 500, message = "用户不存在，请重新登录")
    })
    @PostMapping("/OpusTag")
    public ResultModel<List<TagModel>> OpusTag(@RequestBody Map<String, Integer> Opus) {
        return tagService.OpusTag(Opus.get("opus_id"));
    }



    @ApiOperation(value="获取标签下作品列表", notes="用户点击标签的时候，获取该标签下有多少作品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tags_title", value = "标签的中文名称，前端Flutter需要使用 convert.base64Encode(convert.utf8.encode(tags_title)); 进行Base64编码，然后传递给后端！",
                    required = true, dataType = "String", defaultValue="小夫"),
            @ApiImplicitParam(name = "page", value = "分页的页数", required = true, dataType = "Number", defaultValue="1"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 500, message = "缺少token，请登录"),
            @ApiResponse(code = 500, message = "无效token，请勿伪造token"),
            @ApiResponse(code = 500, message = "用户不存在，请重新登录")
    })
    @PostMapping("/TagOpusList")
    public ResultModel<PagingResponseModel<List<OpusModel>>> TagOpusList(@RequestBody JSONObject params) throws UnsupportedEncodingException {

        byte[] asBytes = Base64.getDecoder().decode((params.get("tags_title").toString()));
        params.put("tags_title", new String(asBytes, "utf-8"));

        if (params.get("page") == null) {
            params.put("page", 1);
        }

        return tagService.TagOpusList(
                params.get("tags_title").toString(),
                Integer.parseInt(params.get("page").toString())
        );
    }
}
